package com.kma.summer2012.client.presenters.manager;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.events.manager.ManageToolsEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.CrumbsCatalogOfTools.CrumbsCatalogOfToolsService;
import com.kma.summer2012.client.services.session.CrumbsSessionService;
import com.kma.summer2012.shared.model.UICrumbsToolItem;
import com.smartgwt.client.widgets.tree.TreeGrid;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 22.02.13
 * Time: 0:34
 * To change this template use File | Settings | File Templates.
 */
public class AddToolPresenter implements Presenter {

    public interface Display {
        void setPageTitle(String title);

        String getToolName();

        void setToolName(String toolName);

        String getToolDescription();

        void setToolDescription(String toolDescription);

        List<String> getWorkList();

        void setWorkList(List<String> list);

        HasClickHandlers getSaveButton();

        HasClickHandlers getCancelButton();

        TreeGrid getCatalog();

        Widget asWidget();
    }

    private final Display display;
    private UICrumbsToolItem item;
    //private Catalog?


    public AddToolPresenter(Display display) {
        this.display = display;
        bind();
        CrumbsSessionService.App.getInstance().getParameterToolItem("toolItem", new AbstractCallBack<UICrumbsToolItem>() {
            @Override
            public void onSuccess(UICrumbsToolItem uiCrumbsToolItem) {
                item = uiCrumbsToolItem;
                fillInfo();
            }
        });
    }

    private void bind() {
        this.display.getSaveButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doSaveTool();
            }
        });
        this.display.getCancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new ManageToolsEvent());
            }
        });

    }

    private void doSaveTool() {
        if (validation()) {
            CrumbsCatalogOfToolsService.App.getInstance().getByNameTool(this.display.getToolName(), new AsyncCallback<UICrumbsToolItem>() {
                @Override
                public void onFailure(Throwable throwable) {

                }

                @Override
                public void onSuccess(UICrumbsToolItem uiCrumbsToolItem) {
                    if (uiCrumbsToolItem != null && !uiCrumbsToolItem.isDelete() && item.getId() != uiCrumbsToolItem.getId()) {
                        Window.alert("Tool with such name is already exist. Please enter another name");
                    } else {
                        item.setToolName(AddToolPresenter.this.display.getToolName());
                        item.setToolsDescription(AddToolPresenter.this.display.getToolDescription());
                        item.setWorkList(AddToolPresenter.this.display.getWorkList());
                        CrumbsCatalogOfToolsService.App.getInstance().saveTool(item, new AsyncCallback<Void>() {
                            @Override
                            public void onFailure(Throwable throwable) {
                                Window.alert("AddToolPresenter: can't save tool");
                            }

                            @Override
                            public void onSuccess(Void aVoid) {
                    /*  AddWorkPresenter.this.display.setWorkName("");
                AddWorkPresenter.this.display.setWorkDescription("");
                AddWorkPresenter.this.display.setRunTime("");
                AddWorkPresenter.this.display.setToolsList(new ArrayList<String>());
                CrumbsEventBus.getInstance().fireEvent(new ManageWorksEvent());*/
                                AddToolPresenter.this.display.setToolName("");
                                AddToolPresenter.this.display.setToolDescription("");
                                AddToolPresenter.this.display.setWorkList(new ArrayList<String>());
                                CrumbsEventBus.getInstance().fireEvent(new ManageToolsEvent());
                            }
                        });
                    }
                }
            });


        }
    }

    private void fillInfo() {
        if (item.getId() != 0) {
            this.display.setToolName(item.getToolName());
            this.display.setToolDescription(item.getToolsDescription());
            this.display.setWorkList(item.getWorkList());
        }
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }


    protected boolean validation() {
        String alert = "";

        if (this.display.getToolName().equals(""))
            alert = "Please input name of a tool";


        //Перевірити, чи нема вже tool з такою назвою!
        if (!alert.equals("")) {
            Window.alert(alert);
            return false;
        }
        return true;
    }
}